package com.kyg.tjtoolmanager.controller;

import com.kyg.tjtoolmanager.db.SQLiteUtils;
import com.kyg.tjtoolmanager.module.Goods;
import com.kyg.tjtoolmanager.module.Log;
import com.kyg.tjtoolmanager.module.Reader;
import com.kyg.tjtoolmanager.utils.Utils;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.util.Callback;

import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;


public class LogController extends BaseController {

    @FXML
    TableView<Log> tbViewLogs;
    @FXML
    TableColumn<Log, String> tbcTime, tbcMsg;
    private ObservableList<Log> data = FXCollections.observableArrayList();

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        super.initialize(url, resourceBundle);
        reloadLogs();
    }

    @Override
    public void onShow() {
        super.onShow();
        double rootW = rootLayout.getWidth() - 40;
        double rootH = rootLayout.getHeight();
        tbViewLogs.setPrefWidth(rootW);
        tbViewLogs.setPrefHeight(rootH - 120);
        tbcTime.setPrefWidth(140);
        tbcMsg.setPrefWidth(rootW - tbcTime.getPrefWidth() - 20);
    }

    public void reloadLogs() {
        List<Log> logList = SQLiteUtils.getLogs();
        data.clear();
        data.addAll(logList);
        tbViewLogs.getItems().clear();
        tbViewLogs.getItems().addAll(data);
        //设置该列的数据类型-返回单元格内容
        tbcTime.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Log, String>, ObservableValue<String>>() {
            @Override
            public ObservableValue<String> call(TableColumn.CellDataFeatures<Log, String> param) {
                SimpleStringProperty name = new SimpleStringProperty(param.getValue().time);
                return name;
            }
        });

        // 设置imageCol的单元格值获取器
        Callback<TableColumn<Log, String>, TableCell<Log, String>> cellFactory = (col) -> {
            final TableCell<Log, String> cell = new TableCell<>() {
                private final TextArea textArea = new TextArea();

                @Override
                protected void updateItem(String item, boolean empty) {
                    super.updateItem(item, empty);

                    if (!empty && item != null) {
                        try {
                            if (item.length() < 100 || item.contains("\n")) {
                                Label textArea = new Label();
                                textArea.setText(item);
                                setGraphic(textArea);
                            } else {
                                textArea.setPrefHeight(30);
                                textArea.setText(item);
                                setGraphic(textArea);
                            }
                        } catch (IllegalArgumentException e) {
                            System.out.println("Invalid Base64 string." + e.getMessage());
                        }
                    } else {
                        setText(null);
                        setGraphic(null);
                    }
                }
            };
            return cell;
        };
        tbcMsg.setCellFactory(cellFactory);
        tbcMsg.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Log, String>, ObservableValue<String>>() {
            @Override
            public ObservableValue<String> call(TableColumn.CellDataFeatures<Log, String> param) {
                SimpleStringProperty name = new SimpleStringProperty(param.getValue().log);
                return name;
            }
        });
    }

    @FXML
    protected void onLogCleanClick() {
        //日志
        SQLiteUtils.cleanLog();
        reloadLogs();
    }

}